import axios from 'axios'
import {ElMessage} from 'element-plus'
import router from '../../router/index'

const request = axios.create({
    baseURL: '/api',
    timeout: 5000
    // headers: {'X-Custom-Header': 'foobar'}
    // withCredentials: true // 允许携带cookie
})
request.interceptors.response.use( 
    response => {

              return response.data
   
    },
    error => {
        if (error.response.status === 401) {
        ElMessage.error('登录失效, 请重新登录')
        router.push('/login')
        }
        return Promise.reject(error)
    }
)
request.interceptors.request.use(
    config => {
        let loginUser=JSON.parse(localStorage.getItem('loginUser'))
        if(loginUser){
            config.headers.token=loginUser.token
        }
        return config
    }
 )
export default request